package com.vg.leftmenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.androidhive.jsonparsing.LvAdapter_contact;
import com.androidhive.jsonparsing.LvAdapter_status;
import com.androidhive.jsonparsing.R;
import com.vg.library.Httppostaux;
import com.vg.service.return_contact_array;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class contact extends Activity {

	ArrayList<HashMap<String, String>> contactlist,contaclis_after;
	ListView lv;
	return_contact_array mtest;
	Httppostaux post;
	LvAdapter_contact adapter;
	EditText edit;
	
	com.vg.Dialog.AlertDialogManager alert = new com.vg.Dialog.AlertDialogManager();
	
	// Progress Dialog
	private ProgressDialog pDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		post=new Httppostaux();
		lv=(ListView)findViewById(R.id.contact_lvContact);
		edit=(EditText)findViewById(R.id.edit_contact_find);
		//LoadGUI();
		new LoadTracks().execute();
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		// tao list mới;
		//update_listview();
		
		
			
		//
		//
				//int logstatus = -1;
		
				/*
				 * Creamos un ArrayList del tipo nombre valor para agregar los
				 * datos recibidos por los parametros anteriores y enviarlo
				 * mediante POST a nuestro sistema para relizar la validacion
				 * contac
				 */
		
	
				edit.addTextChangedListener(new TextWatcher() {
					
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub
						//contact.this.adapter.
						//update_listview();
					}
					
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						
					}
				});
	}
	
	private void LoadGUI() 
	{
		contactlist=new ArrayList<HashMap<String,String>>();
		ArrayList<NameValuePair> postparameters2send = new ArrayList<NameValuePair>();

		postparameters2send.add(new BasicNameValuePair("email",
				"traito9x@gmail.com"));
		JSONArray jdata = post.getserverdata(postparameters2send,
				"http://deduocyeu.com/json/friend_list.php");
		Log.i("log_tag:",""+jdata.toString());
		//SystemClock.sleep(950);
		// si lo que obtuvimos no es null
		for(int i=0;i<=jdata.length();i++)
		 {
			//Log.i("log_tag_jdata:",""+jdata.toString());
			try {
				JSONObject obj=jdata.getJSONObject(i);
				String name=obj.getString("use_fullname");
				String use_status=obj.getString("use_status");
				//String image=obj.getString("use_avatar");
				
				HashMap<String, String> map=new HashMap<String, String>();
				map=new HashMap<String, String>();
				map.put("use_fullname", name);
				map.put("use_status", use_status);
				//map.put("use_avatar", image);		
				contactlist.add(map);
				
				Log.i("contactlist", ""+contactlist.toString());
				
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		 } 
		LvAdapter_contact adapter=new LvAdapter_contact(this, contactlist);
		lv.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	public void update_listview()
	{
		String tempTextSearch = edit.getText().toString().trim()
				.toLowerCase();
		contaclis_after=new ArrayList<HashMap<String,String>>();
		for (int i = 0; i < contactlist.size(); i++) {
//			if (contactlist.get(i).getName().toLowerCase().contains(tempTextSearch)) {
//				tempArray.add(array.get(i));
			if (contactlist.get(i).containsValue(tempTextSearch)) {
				contactlist.add(contactlist.get(i));
			}
		}
		// refresh listview
		refesh();
	}
	
	public void refesh()
	{
		LvAdapter_contact adapter=new LvAdapter_contact(this, contaclis_after);
		lv.setAdapter(adapter);
	}
	
	class LoadTracks extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(contact.this);
			pDialog.setMessage("Loading ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		/**
		 * getting tracks json and parsing
		 * */
		protected String doInBackground(String... args) {
			// Building Parameters
			contactlist=new ArrayList<HashMap<String,String>>();
			ArrayList<NameValuePair> postparameters2send = new ArrayList<NameValuePair>();

			postparameters2send.add(new BasicNameValuePair("email",
					"traito9x@gmail.com"));
			JSONArray jdata = post.getserverdata(postparameters2send,
					"http://deduocyeu.com/json/friend_list.php");
			Log.i("log_tag:",""+jdata.toString());
			//SystemClock.sleep(950);
			// si lo que obtuvimos no es null
			for(int i=0;i<=jdata.length();i++)
			 {
				//Log.i("log_tag_jdata:",""+jdata.toString());
				try {
					JSONObject obj=jdata.getJSONObject(i);
					String name=obj.getString("use_fullname");
					String use_status=obj.getString("use_status");
					//String image=obj.getString("use_avatar");
					
					HashMap<String, String> map=new HashMap<String, String>();
					map=new HashMap<String, String>();
					map.put("use_fullname", name);
					map.put("use_status", use_status);
					//map.put("use_avatar", image);		
					contactlist.add(map);
					
					Log.i("contactlist", ""+contactlist.toString());
					
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			 }
			return null; 
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all tracks
			pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					/**
					 * Updating parsed JSON data into ListView
					 * */
					LvAdapter_contact adapter=new LvAdapter_contact(getApplicationContext(), contactlist);
					lv.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					
				}
			});

		}

	}
}
